/*
* @Author: ZHELIHAO\fangshuqin
* @Date:   2018-05-19 11:35:01
* @Last Modified by:   ZHELIHAO\fangshuqin
* @Last Modified time: 2018-05-26 20:54:58
*/
{
	let EVENT_CLICK = Laya.Event.CLICK;
	class IconAndFrame extends ui.Alert.IconAndFrameUI{

		constructor(iconChange,frameChange) {
			super();

			this.init();
			this.iconChange = iconChange;
			this.frameChange = frameChange;
		}

		init() {
			this.allIconList  = null;
			this.allFrameList = null;
			this.tabSelect = 0;

			this.iconNo = 1;
			this.frameNo = 1;
			this.tab_nav.selectedIndex = 0;
            this.tab_con.selectedIndex = 0;

			this.registerActions();
			this.initEvent();
		}


        registerActions() {

            this.ACTIONS = {
                [GAME_CMDS.USETICONANDFRAMELIST] : this.iconAndFrameList,
                [GAME_CMDS.CHANGEICONFRAME]:    this.changeIconOrFrame
            };
            Sail.io.register(this.ACTIONS, this);
        }

		initEvent() {
			// 头像
			this.list_icon_all.selectEnable = true;
            this.list_icon_all.selectHandler = Laya.Handler.create(this, this.singleIcon, null, false);

            // 头像框
            this.list_frame_all.selectEnable = true;
            this.list_frame_all.selectHandler = Laya.Handler.create(this, this.singleFrame, null, false);

            // tab
			this.tab_nav.selectHandler = new Laya.Handler(this, (index) => {
				this.tab_con.selectedIndex = index;
				this.tabSelect = index;
            });

            this.btn_confirm.on(EVENT_CLICK, this, ()=>{
            	// type 0 头像 1 边框
            	// no 编号

            	let type = this.tabSelect;
            	let no =  this.iconNo;
            	if(this.tabSelect) {
            		no = this.frameNo;
            	}
            	Sail.io.emit(GAME_CMDS.CHANGEICONFRAME, {type: this.tabSelect, no: no});
            	
            	this.close();
				
            })
		}

		iconAndFrameList(data, code) {
			if( code!==0 ){return};

			//头像
			let iconList = data.icon;

			this.allIconList = Sail.store.getUserIcon();

			for(let i=0; i<this.allIconList.length; i++ ){
				if(iconList.indexOf(this.allIconList[i].no) === -1){
					this.allIconList[i].had = 0;
				}else{
					this.allIconList[i].had = 1;
				}
			}
			let resultIcon = [];
			this.allIconList.forEach((item,index)=>{
				let icon_no = Number(item.no);
				resultIcon.push({
					check_icon: {
						skin: `res/alert/iconframe/icon_${icon_no}.png`
					},
					shadowicon:{
						visible: item.had ===1 ? false : true
					}
				})
			})

			this.list_icon_all.array = resultIcon;

			// 头像框
			let frameList = data.frame;
			this.allFrameList = Sail.store.getUserFrame();

			for(let i=0; i<this.allFrameList.length; i++ ){
				if(frameList.indexOf(this.allFrameList[i].no) === -1){
					this.allFrameList[i].had = 0;
				}else{
					this.allFrameList[i].had = 1;
				}
			}

			let resultFrame = [];
			this.allFrameList.forEach((item,index)=>{
				let frame_no = Number(item.no);
				resultFrame.push({
					check_frame: {
						skin: `res/alert/iconframe/frame_${frame_no}.png`
					},
					shadowframe:{
						visible: item.had ===1 ? false : true
					}
				})
			})
			
			this.list_frame_all.array = resultFrame;

		}


		singleIcon(index) {
			let had = this.allIconList[index].had;
			let openType = this.allIconList[index].openType;
			let count 	 =  this.allIconList[index].count;
			let desc     = '';
			switch(openType){
				case '1':
				desc = '用户等级达到' + count + '级才可以解锁';
				break;
				case '2':
				desc = 'vip等级达到' + count + '级才可以解锁';
				break;
				case '3':
				desc = '英雄数量达到' + count + '级才可以解锁';
				break;
			}

            if(!had) {
            	Sail.director.popScene(new Alert.Tips(desc));
            	return;
            }
            this.iconNo = index+1;
           

		}

		singleFrame(index) {
			let had = this.allFrameList[index].had;
			let openType = this.allFrameList[index].openType;
			let count 	 =  this.allFrameList[index].count;
			let desc     = '';
			switch(openType){
				case '1':
				desc = '用户等级达到' + count + '级才可以解锁';
				break;
				case '2':
				desc = 'vip等级达到' + count + '级才可以解锁';
				break;
				case '3':
				desc = '英雄数量达到' + count + '级才可以解锁';
				break;
			}

            if(!had) {
            	Sail.director.popScene(new Alert.Tips(desc));
            	return;
            }
            this.frameNo = index +1;
            
		}

		changeIconOrFrame(data, code,msg) {
			if( code!==0 ) return;
			if(this.tabSelect == 0){
				this.iconChange && this.iconChange(data);
			}else if(this.tabSelect == 1){
				this.frameChange && this.frameChange(data);
			}
			Sail.io.unregister(this.ACTIONS);
		}

	}

	Sail.class(IconAndFrame, 'Alert.IconAndFrame');

}	